package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xrit.sunnyday.dao.MemberDao;
import com.xrit.sunnyday.dao.OrderDao;
import com.xrit.sunnyday.dao.OrderSettingDao;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.Member;
import com.xrit.sunnyday.pojo.Order;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 订单管理
 */
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 提交订单：
     */
    @Override
    @Transactional
    public Order submitOrder(Map<String, String> paraMap) throws SunnydayException {
        //1.获取前端填写的订单日期
        String orderDateStr = paraMap.get("orderDate");
        //2.1转换我们所需要参数的类型
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = null;
        try {
            orderDate = sdf.parse(orderDateStr);
        } catch (ParseException e) {
            //2.2如果转换不成功：抛异常
            throw new SunnydayException("日期格式错误");
        }
        System.out.println("22222222222222222222222");
        //2.通过日期查询预约设置是否存在 t_ordersetting，参数为Date类型
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        //3.判断是否存在该日期能否预约
        System.out.println("33333333333333");
        if(null == orderSetting) {
            //3.1不存在，代表不上班：抛异常
            throw new SunnydayException("所选日期，不能预约");
        }
        System.out.println("44444444444444444444");
        //4.存在，上班,则判断当天是否约满
        if(orderSetting.getReservations() >= orderSetting.getNumber()) {
            //4.1如果约满：抛异常
            throw new SunnydayException("所选日期，预约已满");
        }
        System.out.println("555555555555555555555");
        //5.1获取前端填写的手机号码
        String telephone = paraMap.get("telephone");
        System.out.println(telephone);
        //5.没有满：判断是否为会员,通过手机号码查询 t_member
        List<Member> member = memberDao.findByTelephone(telephone);
        //6.判断是否是会员
        System.out.println("6666666666666666666666");
        Member memberv = null;
        if(null == memberv) {
            //6.1 非会员：添加到会员表 获取id
            memberv = new Member();
            memberv.setIdCard(paraMap.get("idCard"));
            memberv.setName(paraMap.get("name"));
            memberv.setPhoneNumber(telephone);
            memberv.setRegTime(new Date());//给系统当前时间
            memberv.setSex(paraMap.get("sex"));

            //6.2 添加会员, 并获取会员id
            memberDao.add(memberv);
        }
        System.out.println("7777777777777777777777777");
        //7.1 准备查询条件
        Order order = new Order();
        order.setMemberId(memberv.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(Integer.valueOf(paraMap.get("setmealId")));
        System.out.println("888888888888888888");
        //7 会员：通过member_id,orderDate, setmeal_id作为条件去查询t_order表，是否重复预约
        List<Order> orderList = orderDao.findByCondition(order);
        //8.判断是否重复预约：
        System.out.println("9999999999999999999999999999");
        if(null != orderList && orderList.size() > 0){
            //8.1存在，重复：抛异常
            throw new SunnydayException("不能重复预约");
        }
        System.out.println("100000000000000000000000");
        //9.1补充订单信息
        order.setOrderType(paraMap.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //9.不存在，没重复,添加订单
        orderDao.add(order);
        //10.更新已预约数量 t_ordersetting
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return order;
    }

    /**
     * 查询订单，通过id查询预约信息
     * @param id
     * @return
     */
    @Override
    public Map findOrderDetailById(int id) {
        return orderDao.findById4Detail(id);
    }
}